package pl.owm.main;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import com.osp.EmployeeListReport;
import com.osp.ProjectListReport;
import com.osp.ProjectManager;
import com.osp.model.Employee;
import com.osp.model.Task;


public class Main {
	static ProjectManager pm;
	static Scanner sc = new Scanner(System.in);
	
	public static void showMenu() {
		System.out.println("\nMenu:");
		System.out.println("1) Lista pracownik\u00F3w");
		System.out.println("2) Lista projekt\u00F3w");
		System.out.println("3) Szczeg\u00F3\u0142owe informacje o pracowniku");
		System.out.println("4) Szczeg\u00F3\u0142y projektu");
		System.out.println("5) Podsumowanie projekt\u00F3w");
		System.out.println("6) Ponownie wczytaj dane");
		System.out.println("7) Zako\u0144cz");
		
	}

	public static void main(String[] args) {

		ModelCreator mc = new ModelCreator();
		pm = mc.createModel();

		showMenu();
		int option = -1;
		do {
			option = -1;
			try {
				option = sc.nextInt();
			} catch (Exception e) {
				System.out.println("Podano bledny numer opcji");
				option = -1;
				sc.nextLine();
			}
			
			switch (option) {
			case 1:
				showWorkersList();
				break;
			case 2:
				showProjectsList();
				break;
			case 3:
				showWorkerDetails();
				break;
			case 4:
				showProjectDetails();
				break;
			case 5:
				System.out.println("wybrano 5");
				showProjectsSummary();
				break;
			case 6:
				reloadData();
				System.out.println("Dane zostaly wczytane ponownie");
				break;
			case 7:
				sc.close();
				return;
			default:
				System.out.println("Podaj poprawny numer opcji");
				break;
			}
			option = -1;
			showMenu();
		} while (true);
	}

	private static void showHelp() {
		// TODO Auto-generated method stub
		
	}

	private static void showProjectsSummary() {
		// TODO Auto-generated method stub
		
	}

	private static void showProjectDetails() {
		String projectName;
		int year;
		
		
		
		try {
			sc.nextLine();
			System.out.print("Podaj nazwe projektu: ");
			projectName = sc.nextLine();

			System.out.print("Podaj rok: ");
			year = sc.nextInt();

			
			List<Employee> employees =  pm.getEmployees();
			
//			Map<employee,Map<month, time>>
			Map<Employee, HashMap<Integer, Integer>> tasks = new HashMap<Employee, HashMap<Integer, Integer>>();
			for (Employee employee : employees) {

// USUNAC
//		Calendar c1 = Calendar.getInstance();
//		c1.set(Calendar.MONTH, Calendar.DECEMBER);
//		Calendar c2 = Calendar.getInstance();
//		c2.set(Calendar.MONTH, Calendar.JANUARY);
//		Calendar c3 = Calendar.getInstance();
//		c3.set(Calendar.MONTH, Calendar.MAY);
//		Calendar c4 = Calendar.getInstance();
//		c4.set(Calendar.MONTH, Calendar.FEBRUARY);
//		Calendar c5 = Calendar.getInstance();
//		c4.set(Calendar.MONTH, Calendar.JULY);
//		Task task1 = new Task(Calendar.getInstance(), "opis", 50, "projA"); //07
//		Task task2 = new Task(c1, "opis2", 60, "projB"); //12
//		Task task3 = new Task(c2, "opis2", 630, "projA"); //01
//		Task task4 = new Task(c3, "opis2", 80, "projB"); //05
//		Task task5 = new Task(c4, "opis3", 1670, "projA"); //02
//		Task task6 = new Task(c5, "opis3", 1000, "projA"); //07
//		employee.addTask(task1);
//		employee.addTask(task2);
//		employee.addTask(task3);
//		employee.addTask(task4);
//		employee.addTask(task5);
//		employee.addTask(task6);
// Do tego miejsca
	
				HashMap<Integer, Integer> tmp = new HashMap<Integer, Integer>();
				if (!tasks.containsKey(employee)) {
					tasks.put(employee, tmp);
				} else {
					tmp = tasks.get(employee);
				}
				for (Task task : employee.getTasks().get(year)) {
					if (task.getProjectName().equals(projectName)) {
						try {
//							System.out.println(task.getDate().get(Calendar.MONTH) + " + " + task.getElapsedMinutes());
							tmp.put(task.getDate().get(Calendar.MONTH), tmp.get(task.getDate().get(Calendar.MONTH)) + task.getElapsedMinutes());
							
						} catch (NullPointerException e) {
							tmp.put(task.getDate().get(Calendar.MONTH), task.getElapsedMinutes());
//							System.out.println(tmp);
						}
					}
				}
			}

			if (tasks.size() != 0) {
				System.out.print(String.format("%-25s", year));

				for (int i = 1; i <= 12; i++) {
					System.out.print(" | " + String.format("%4s", i));
				}
				for (Employee employee : tasks.keySet()) {
					System.out.print("\n" + String.format("%-25s", employee));
					
					for (int i = 0; i <= 11; i++) {
						int time;
						try {
							time = tasks.get(employee).get(i);
						} catch (NullPointerException e) {
							
							time = 0;
						}
						System.out.print(" | " + String.format("%4s", time/60));
					}
				}
			} else {
				System.out.println("Brak danych dla wybranych parametrow");
			}

			
		} catch (Exception e) {
			System.out.println("Podano bledna wartosc");
//			e.printStackTrace();
		}
		
	}

	private static void showWorkerDetails() {
		String employeeName;
		int year;
		
		try {
			sc.nextLine();
			System.out.print("Podaj nazwe pracownika: ");
			employeeName = sc.nextLine();
			Employee employee = null;
			
			for (Employee em : pm.getEmployees()) {
				if (em.getName().equalsIgnoreCase(employeeName)) {
					employee = em;
//					System.out.println(employee);
				}
			}
			
// USUNAC
//			Calendar c1 = Calendar.getInstance();
//			c1.set(Calendar.MONTH, Calendar.DECEMBER);
//			Calendar c2 = Calendar.getInstance();
//			c2.set(Calendar.MONTH, Calendar.JANUARY);
//			Calendar c3 = Calendar.getInstance();
//			c3.set(Calendar.MONTH, Calendar.MAY);
//			Task task1 = new Task(Calendar.getInstance(), "opis", 50, "projA");
//			Task task2 = new Task(c1, "opis2", 60, "projB");
//			Task task3 = new Task(c2, "opis2", 670, "projA");
//			Task task3b = new Task(c2, "opis2", 70, "projB");
//			Task task3c = new Task(c2, "opis2", 90, "projA");
//			Task task4 = new Task(c3, "opis2", 80, "projB");
//			employee.addTask(task1);
//			employee.addTask(task2);
//			employee.addTask(task3);
//			employee.addTask(task3b);
//			employee.addTask(task3c);
//			employee.addTask(task4);
// Do tego miejsca
			
			System.out.print("Podaj rok: ");
			year = sc.nextInt();

			Set<String> projectsList = new HashSet<String>();

			for(Integer rok : employee.getTasks().keySet()){
				System.out.println(rok);
			}
			
			for (Task task : employee.getTasks().get(year) ) {
				projectsList.add(task.getProjectName());
			}
			
//			Map<month,Map<projectName, time>>
			Map<Integer, HashMap<String, Integer>> tasks = new HashMap<Integer, HashMap<String, Integer>>();

			for (Task task : employee.getTasks().get(year) ) {
				HashMap<String, Integer> tmp = new HashMap<String, Integer>();
				try {
					if (tasks.get(task.getDate().get(Calendar.MONTH)).size() > 0) {
						tmp = tasks.get(task.getDate().get(Calendar.MONTH));
					}
					
				} catch (NullPointerException e) {
					tmp = new HashMap<String, Integer>();
					tasks.put(task.getDate().get(Calendar.MONTH), tmp);
				}
				try {
//					System.out.println(tmp.get(task.getProjectName()) + "" + task.getElapsedMinutes());
					tmp.put(task.getProjectName(), tmp.get(task.getProjectName()) + task.getElapsedMinutes());
				} catch (NullPointerException e) {
					String as = task.getProjectName();
					int a = task.getElapsedMinutes();
					tmp.put(task.getProjectName(), task.getElapsedMinutes());
				}
			}


			System.out.print(year);

			for (String proj : projectsList) {
				System.out.print(" | " + String.format("%10s", proj));
			}
			for (Integer month : tasks.keySet()) {
				System.out.print("\n" + String.format("%4s", month + 1));
				
				for (String proj : projectsList) {
					int time;
					try {
						time = tasks.get(month).get(proj);
					} catch (NullPointerException e) {
						time = 0;
					}
					System.out.print(" | " + String.format("%10s", time/60));
				}
				
			}
			
		} catch (Exception e) {
			System.err.println("Podano bledna wartosc");
//			e.printStackTrace();
			System.out.println("");
			System.err.flush();
			
		}
		
	}

	private static void showProjectsList() {
		ProjectListReport projectReport = new ProjectListReport();
		
		int i = 0;
		for (String project : projectReport.getProjectList(pm)) {
			System.out.println(i +  ": " + project);
			i++;
		}
		
	}

	private static void showWorkersList() {
		try {
			EmployeeListReport elr = new EmployeeListReport();
			List<String> emlopyeeList = elr.getEmployeeList(pm);
			int i = 0;
			for (String employee : emlopyeeList) {
				System.out.println(i +  ": " +employee);
				i++;
			}
		} catch (NullPointerException e) {
			System.err.println("Brak pracownikow w liscie. " );
		}
		
	}

	private static void reloadData() {
		
		ModelCreator mc = new ModelCreator();
		pm = mc.createModel();
	}

}
